#include "isr.h"
#include "Global.h"
#include "my_systick.h"

volatile uint32_t g_ticks = 0xFFFFFFFFU;

ATE_FRAME_t pData = {0};
ISR_InitTypeDef_t IRQ_Fun;

/**
  * @brief  SysTick Handler Decreament the g_ticks value
  * @param  TM13 IRQ
  * @retval None
  */
void SysTick_Handler(void)
{
  Systick_Irq_Handler();
}

/**
  * @brief  IRQ00_Handler
  * @param  LVI IRQ
  * @retval None
  */
//void IRQ00_Handler(void)
//{
//	IRQ_Fun.IRQ[LVI_IRQn].intHandle(&pData);
//}

/**
  * @brief  IRQ01_Handler
  * @param  INTP0 IRQ
  * @retval None
  */
void IRQ01_Handler(void)
{
	IRQ_Fun.IRQ[1].intHandle(&pData);
}

/**
  * @brief  IRQ02_Handler
  * @param  INTP1 IRQ
  * @retval None
  */
void IRQ02_Handler(void)
{
	IRQ_Fun.IRQ[2].intHandle(&pData);
}

/**
  * @brief  IRQ03_Handler
  * @param  INTP2 IRQ
  * @retval None
  */
void IRQ03_Handler(void)
{
	IRQ_Fun.IRQ[3].intHandle(&pData);
}

/**
  * @brief  IRQ04_Handler
  * @param  INTP3 IRQ
  * @retval None
  */
void IRQ04_Handler(void)
{
	IRQ_Fun.IRQ[4].intHandle(&pData);
}

/**
  * @brief  IRQ05_Handler
  * @param  INTTM01H IRQ
  * @retval None
  */
void IRQ05_Handler(void)
{
	IRQ_Fun.IRQ[5].intHandle(&pData);
}

/**
  * @brief  IRQ06_Handler
  * @param  KEY IRQ
  * @retval None
  */
void IRQ06_Handler(void)
{
	IRQ_Fun.IRQ[6].intHandle(&pData);
}

/**
  * @brief  IRQ07_Handler
  * @param  ST2/SPI20/IIC20 IRQ 
  * @retval None
  */
void IRQ07_Handler(void)
{
	IRQ_Fun.IRQ[7].intHandle(&pData);
}

/**
  * @brief  IRQ08_Handler
  * @param  SR2/SPI21/IIC21 IRQ
  * @retval None
  */
void IRQ08_Handler(void)
{	
	IRQ_Fun.IRQ[8].intHandle(&pData);
}

/**
  * @brief  IRQ09_Handler
  * @param  SRE2 IRQ
  * @retval None
  */
void IRQ09_Handler(void)
{
	IRQ_Fun.IRQ[9].intHandle(&pData);
}

/**
  * @brief  IRQ10_Handler
  * @param  ST0/SPI00/IIC00 IRQ  
  * @note   the num of ST0_IRQn is same as SPI00_IRQn and IIC00_IRQ, so SPI00_IRQn or IIC00_IRQ
  *			interrupt quote this function handler IRQ10_Handler
  * @retval None
  */
void IRQ10_Handler(void)
{
	IRQ_Fun.IRQ[10].intHandle(&pData);
}

/**
  * @brief  IRQ11_Handler
  * @param  SR0/SPI01/IIC01 IRQ 
  * @note   the num of SR0_IRQn is same as SPI01_IRQn and IIC01_IRQ, so SPI01_IRQn or IIC01_IRQ
  *			interrupt quote this function handler IRQ11_Handler
  * @retval None
  */
void IRQ11_Handler(void)
{
	IRQ_Fun.IRQ[11].intHandle(&pData);	
}

/**
  * @brief  IRQ12_Handler
  * @param   SRE0 or TM01H IRQ 
  * @note   the num of SRE0_IRQn is same as TM01H_IRQn, so TM01H_IRQn 
  *			interrupt quotes this function handler IRQ12_Handler
  * @retval None
  */
void IRQ12_Handler(void)
{
	IRQ_Fun.IRQ[12].intHandle(&pData);	
}

/**
  * @brief  IRQ13_Handler
  * @param  ST1/SPI10/IIC10 IRQ  
  * @note   the num of ST1_IRQn is same as SPI10_IRQn and IIC10_IRQ, so SPI10_IRQn or IIC10_IRQ
  *			interrupt quote this function handler IRQ13_Handler
  * @retval None
  */
void IRQ13_Handler(void)
{
	IRQ_Fun.IRQ[13].intHandle(&pData);	
}

/**
  * @brief  IRQ14_Handler
  * @param  SR1/SPI11/IIC11 IRQ
  * @note   the num of SR1_IRQn is same as SPI11_IRQn and IIC11_IRQ, so SPI11_IRQn or IIC11_IRQ
  *			interrupt quote this function handler IRQ14_Handler
  * @retval None
  */
void IRQ14_Handler(void)
{
	IRQ_Fun.IRQ[14].intHandle(&pData);	
}

/**
  * @brief  IRQ15_Handler
  * @param  SRE1 IRQ
  * @retval None
  */
void IRQ15_Handler(void)
{
	IRQ_Fun.IRQ[15].intHandle(&pData);	
}

/**
  * @brief  IRQ16_Handler
  * @param  IICA IRQ
  * @retval None
  */
void IRQ16_Handler(void)
{
	IRQ_Fun.IRQ[16].intHandle(&pData);	
}

/**
  * @brief  IRQ17_Handler
  * @param  TM00 IRQ
  * @retval None
  */
void IRQ17_Handler(void)
{
	IRQ_Fun.IRQ[17].intHandle(&pData);	
}

/**
  * @brief  IRQ18_Handler
  * @param  TM01 IRQ
  * @retval None
  */
void IRQ18_Handler(void)
{
	IRQ_Fun.IRQ[18].intHandle(&pData);	
}

/**
  * @brief  IRQ19_Handler
  * @param  TM02 IRQ
  * @retval None
  */
void IRQ19_Handler(void)
{
	IRQ_Fun.IRQ[19].intHandle(&pData);	
}

/**
  * @brief  IRQ20_Handler
  * @param  TM03 IRQ
  * @retval None
  */
void IRQ20_Handler(void)
{
	IRQ_Fun.IRQ[20].intHandle(&pData);	
}

/**
  * @brief  IRQ21_Handler
  * @param  ADC IRQ
  * @retval None
  */
void IRQ21_Handler(void)
{
	IRQ_Fun.IRQ[21].intHandle(&pData);	
}

/**
  * @brief  IRQ22_Handler
  * @param  RTC IRQ
  * @retval None
  */
void IRQ22_Handler(void)
{
	IRQ_Fun.IRQ[22].intHandle(&pData);	
}

/**
  * @brief  IRQ23_Handler
  * @param  IT IRQ
  * @retval None
  */
void IRQ23_Handler(void)
{
	IRQ_Fun.IRQ[23].intHandle(&pData);	
}

/**
  * @brief  IRQ24_Handler
  * @param  OCRV IRQ
  * @retval None
  */
void IRQ24_Handler(void)
{
	IRQ_Fun.IRQ[24].intHandle(&pData);	
}


/**
  * @brief  IRQ27_Handler
  * @param  TM10 IRQ
  * @retval None
  */
void IRQ27_Handler(void)
{
	IRQ_Fun.IRQ[27].intHandle(&pData);	
}

/**
  * @brief  IRQ28_Handler
  * @param  TM13 IRQ
  * @retval None
  */
void IRQ28_Handler(void)
{
	IRQ_Fun.IRQ[28].intHandle(&pData);	
}

/**
  * @brief  IRQ29_Handler
  * @param  TM12 IRQ
  * @retval None
  */
void IRQ29_Handler(void)
{
	IRQ_Fun.IRQ[29].intHandle(&pData);	
}

/**
  * @brief  IRQ30_Handler
  * @param  TM13 IRQ
  * @retval None
  */
void IRQ30_Handler(void)
{
	IRQ_Fun.IRQ[30].intHandle(&pData);	
}

/**
  * @brief  IRQ31_Handler
  * @param  FMC IRQ 
  * @retval None
  */
void IRQ31_Handler(void)
{
	IRQ_Fun.IRQ[31].intHandle(&pData);	
}


/**
  * @brief  register interrupt function according to  IRQn num.
  * @param  irq_num: it relates to irq handler 
  * @param	interrupt:interrupt server function
  * @retval regiseter status 
  */
int ISR_Register(IRQn_Type irq_num, void *interrupt)
{
	int res = ISR_SUCC;
	IRQn_Type IRQ_Channel;

	if(IRQ_Fun.IRQ[irq_num].IRQ_Flag)
	{
		return ISR_ERR;
	}
	else
	{		
		if(irq_num > FMC_IRQn)
		{
			IRQ_Channel = (IRQn_Type)(irq_num % 32);
		}
		else
		{
			IRQ_Channel = irq_num;
		}
		IRQ_Fun.IRQ[irq_num].IRQ_Flag = 1;
		IRQ_Fun.IRQ[IRQ_Channel].intHandle = (isrFun_t)interrupt;
		INTC_ClearPendingIRQ(irq_num); // clear  interrupt flag 
		NVIC_ClearPendingIRQ(irq_num); // clear  interrupt flag 
		INTC_EnableIRQ(irq_num);
	}

	return res;
}
